/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.mainmenu;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.GameManager;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.player.SimplePlayer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author Massimo Canonaco
 *
 */
public class LoadProfileDialog extends PlayerInfoDailog {

	/** */
	private JComboBox<String> profiles;
	
	/**
	 * @param container
	 * @param infoMap
	 * @param factory
	 * @throws IOException 
	 */
	public LoadProfileDialog(MainMenuPanel container, Map infoMap, OverallGUILayoutFactory factory) throws IOException {
		
		super(container, infoMap, factory);
		final String TEXT_COLOR = "dialog.text.color", 
					TEXT_STROKE_COLOR = "dialog.textstroke.color",
					TEXT_STROKE_SIZE = "dialog.textstroke.size";
		
		Color 	strokeColor = factory.getParsedColor((String)infoMap.get(TEXT_STROKE_COLOR)), 
				textColor = factory.getParsedColor((String)infoMap.get(TEXT_COLOR));
		
		float  strokeSize = Float.parseFloat((String)infoMap.get(TEXT_STROKE_SIZE));
		backButton.setEnabled(true);
		profiles = new JComboBox<>();
		
		JLabel info = new JLabel(new ImageIcon(factory.createTextImage("Carica", fontSize, factory.getHeaderFont(), strokeSize, textColor, strokeColor)));
		profiles.setPreferredSize(new Dimension(250, 22));
		profiles.setSize(250, 22);
		add(info, layoutConstraints);
		add(profiles, layoutConstraints);
		
		JPanel buttons = new JPanel(new FlowLayout());
		buttons.setOpaque(false);
		goButton.setEnabled(true);
		buttons.add(backButton);
		buttons.add(goButton);
		add(buttons,layoutConstraints);
	}
	
	@Override
	public void showDialog(Component parent, String title) {
		List<String> existingProfiles = getProfilesList();
		profiles.removeAllItems();
		for (String profile : existingProfiles) 
		{
			profiles.addItem(profile);
		}
		super.showDialog(parent, title);
	}
	
	@Override
	public void next() 
	{
		playerName = (String) profiles.getSelectedItem();
		GameManager gameManager = parent.getParentMainFrame().getGameManager();
		SimplePlayer clientPlayer = gameManager.getClientPlayer();
		gameManager.getProfileLoader().load(clientPlayer, playerName, gameManager.getStatsFactory(),true);
		gameManager.getClientBattlePlayer().setNickname(clientPlayer.getNickname());
		gameManager.getClientBattlePlayer().setNickname(clientPlayer.getNickname());
		super.next();
	}
}
